//
//  BANANAViewController.m
//  DeepBreathing
//
//  Created by rimi on 15/12/15.
//  Copyright © 2015年 rimi. All rights reserved.
//

#import "BANANAViewController.h"
#import "APPLEViewController.h"

@interface BANANAViewController ()

@property(nonatomic,strong)NSArray *viewControllers;

@property(nonatomic,strong)NSArray *normalImages;

@property(nonatomic,strong)NSArray *selectedImages;

@property(nonatomic,strong)UIView *viewController123;

@property(nonatomic,strong)UIView *buttonContainner;

//-(void)initAddview;

@end

enum {
    ButtonTag = 100,
    ImageTag = 200,
};
//全局静态
//static const NSInteger buttonTag_ = 100;
@implementation BANANAViewController

//#define BUTTON_TAG 100


#pragma mark -- initialize

-(instancetype)initWithViewControllers:(NSArray *)viewControllers normalStateImage:(NSArray *)normalImages selectedStateImage:(NSArray *)selectedImages{
    
    self= [super init];
    if (self) {
        
        self.viewControllers = viewControllers;
        self.normalImages = normalImages;
        self.selectedImages = selectedImages;
        
    }
    
    return self;
}
-(instancetype)initWithViewControllers:(NSArray *)viewControllers
{
    return [self initWithViewControllers:viewControllers normalStateImage:nil selectedStateImage:nil];
}
//生命周期
#pragma mark -- life cycle

//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    
//    
//    APPLEViewController *vc = (APPLEViewController *)self.parentViewController;
//    
//    [vc openLeftDrawer];
//
//    
//    
//}
//
//-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    APPLEViewController *vc = (APPLEViewController *)self.parentViewController;
//    
//    [vc closeLeftDrawer];
//
//    
//}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:1.0 green:0.7767 blue:0.9585 alpha:1.0];
    [self initAddview];
   
    
    
    
    
}

#pragma mark -- overwhrite

#pragma mark -- pravite methods
-(void)initAddview{
    
    [self.view addSubview:self.viewController123];
    [self.view addSubview:self.buttonContainner];

    
    [self.viewControllers enumerateObjectsUsingBlock:^(UIViewController *vc, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        UIImage *nolmoalImage = self.normalImages[idx];
        
        UIImage *selestedImage = self.selectedImages[idx];
        
        [button setImage:nolmoalImage forState:UIControlStateNormal];
        
        [button setImage:selestedImage forState:UIControlStateSelected];
        
        
        CGFloat width = 375.f/self.viewControllers.count;
        
        CGFloat height = width/(nolmoalImage.size.width/nolmoalImage.size.height);
        
        button.frame = CGRectMake(width * idx, 0, width, height);
        
        [button addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
         button.tag = ButtonTag + idx;

        
        [self.buttonContainner addSubview:button];
        
    }];
    self.selestedIndex = 0;
}

#pragma mark --callback/action

-(void)respondsToButton:(UIButton *)sender
{
//    sender.selected = !sender.selected;
    self.selestedIndex = sender.tag - ButtonTag;
}

#pragma mark -- interface methods

#pragma mark -- system protocol

#pragma mark -- custom protocol

#pragma mark -- setter


-(void)setSelestedIndex:(NSInteger)selestedIndex
{
    
    //移除
    UIViewController *currenViewController = self.viewControllers[_selestedIndex];
    if (currenViewController.parentViewController) {
        [currenViewController.view removeFromSuperview];
        [currenViewController
         removeFromParentViewController];
        [currenViewController
         willMoveToParentViewController:nil];
        
        UIButton *cuurentButton = (UIButton *)[self.buttonContainner viewWithTag:ButtonTag + _selestedIndex];
        cuurentButton.selected = NO;
        
        
    }
    

   
    _selestedIndex = selestedIndex;
    
    UIViewController *controller = self.viewControllers[selestedIndex];
    //关联controller
    [self addChildViewController:controller];
    //添加视图层级
    [self.viewController123 addSubview:controller.view];
    
    [controller didMoveToParentViewController:self];
    
    UIButton *button = (UIButton *)[self.buttonContainner viewWithTag:ButtonTag + _selestedIndex];
    
    button.selected = YES;
    
}

#pragma mark -- getter

-(UIView *)viewController123{
    
    if (!_viewController123) {
        
        _viewController123 = [[UIView alloc]initWithFrame:self.view.bounds];
    }
    
    return _viewController123;
}
-(UIView *)buttonContainner{
    
    CGFloat height = 375.f/self.viewControllers.count/1.28;
    
    if (!_buttonContainner) {
        _buttonContainner = [[UIView alloc]initWithFrame:CGRectMake(0, 667 - height, 375.f, height)];
    }
    return _buttonContainner;
}




@end
